﻿<%@ Page AutoEventWireup="true" CodeBehind="ChangePassword.aspx.cs" Inherits="AssetManage.Account.ChangePassword" Language="C#" MasterPageFile="~/Site.Master" Title="修改密码" %>

<%@ MasterType VirtualPath="~/Site.Master" %>

<asp:Content ContentPlaceHolderID="MainContent" runat="server">
<h2><%: Title %></h2>
<script type="text/javascript" src="/Scripts/CryptoJS/sha256.js"></script>
<script type="text/javascript" src="/Scripts/CryptoJS/enc-base64-min.js"></script>
<script type="text/javascript">
    var CurrentPassword = "#<%= CurrentPassword.ClientID %>";
    var CurrentPassword_Error = "#<%= CurrentPassword_Error.ClientID %>";

    var NewPassword = "#<%= NewPassword.ClientID %>";
    var NewPassword_Error = "#<%= NewPassword_Error.ClientID %>";

    var ConfirmNewPassword = "#<%= ConfirmNewPassword.ClientID %>";
    var ConfirmNewPassword_Error = "#<%= ConfirmNewPassword_Error.ClientID %>";

    var Captcha = "#<%= Captcha.ClientID %>";
    var Captcha_Error = "#<%= Captcha_Error.ClientID %>";

    var ChangePassword = "#ChangePassword";

    $(function() {
        $(ChangePassword).attr("disabled", "disabled");

        $(CurrentPassword).change(function() {
            $(ChangePassword).attr("disabled", "disabled");

            if ($(CurrentPassword).val() === "") {
                $(CurrentPassword_Error).text("请输入当前密码。");
            } else {
                var reg = /^([a-zA-Z0-9-`=\\\[\];',./~!@#$%\^&*()_+|{}:"<>?]){6,16}$/;
                if (!reg.test($(CurrentPassword).val())) {
                    $(CurrentPassword_Error).text("请输入正确的当前密码。");
                } else {
                    if (HaveNoError()) {
                        $(ChangePassword).removeAttr("disabled");
                    }
                }
            }
        });

        $(NewPassword).change(function() {
            $(ChangePassword).attr("disabled", "disabled");

            if ($(NewPassword).val() === "") {
                $(NewPassword_Error).text("请输入新密码。");
            } else {
                var reg = /^([a-zA-Z0-9-`=\;',.~!@#$%^&*_+:"?]){6,16}$/;
                if (!reg.test($(NewPassword).val())) {
                    $(NewPassword_Error).text("请输入正确的新密码。");
                } else {
                    if (HaveNoError()) {
                        $(ChangePassword).removeAttr("disabled");
                    }
                }
            }
        });

        $(ConfirmNewPassword).change(function() {
            $(ChangePassword).attr("disabled", "disabled");

            if ($(ConfirmNewPassword).val() === "") {
                $(ConfirmNewPassword_Error).text("请再次输入新密码。");
            } else {
                if ($(NewPassword).val() !== $(ConfirmNewPassword).val()) {
                    $(ConfirmNewPassword_Error).text("两次输入的新密码不匹配。");
                } else {
                    $(ConfirmNewPassword_Error).text("");
                    if (HaveNoError()) {
                        $(ChangePassword).removeAttr("disabled");
                    }
                }
            }
        });

        $(Captcha).change(function() {
            $(ChangePassword).attr("disabled", "disabled");

            if ($(Captcha).val() === "") {
                $(Captcha_Error).text("请输入验证码。");
            } else {
                var reg = /^[^jiloxJILOX10]{5}$/;
                if (!reg.test($(Captcha).val())) {
                    $(Captcha_Error).text("请输入正确的验证码。");
                } else {
                    var sendData = {
                        section: "captcha",
                        captcha: $(Captcha).val()
                    };
                    $.ajax({
                        type: "POST",
                        url: "/Handler/Validate",
                        data: sendData,
                        beforeSubmit: function() {
                            $(Captcha_Error).text("正在验证……");
                        },
                        success: function(response) {
                            var responseData = eval("(" + response + ")");
                            if (responseData.success) {
                                $(Captcha_Error).text("");
                                if (HaveNoError()) {
                                    $(ChangePassword).removeAttr("disabled");
                                }
                            } else {
                                $(Captcha_Error).text("请输入正确的验证码。");
                                $(ChangePassword).attr("disabled", "disabled");
                                if (responseData.error === "Cookie_Error") {
                                    window.location.replace("/Error/Cookie");
                                }
                            }
                        }
                    });
                }
            }
        });

        $("#CaptchaImg").click(function() {
            $("#CaptchaImg").attr("src", "/Handler/Captcha?action=get&random=" + Math.random());
            $(Captcha).val("");
            $(Captcha_Error).text("请输入验证码。");
        });

        $(ChangePassword).click(function() {
            if (!HaveNoError()) {
                $(ChangePassword).attr("disabled", "disabled");
                return;
            }

            $(ChangePassword).attr("value", "正在提交……");
            $(ChangePassword).attr("disabled", "disabled");

            var sendData = {
                "action": "changepassword",
                "currentpassword": CryptoJS.SHA256($(CurrentPassword).val()).toString(CryptoJS.enc.Hex).toUpperCase(),
                "newpassword": CryptoJS.SHA256($(NewPassword).val()).toString(CryptoJS.enc.Hex).toUpperCase(),
                "captcha": $(Captcha).val()
            };
            $.ajax({
                type: "POST",
                url: "/Handler/Account",
                data: sendData,
                success: function(response) {
                    var responseData = eval("(" + response + ")");
                    if (responseData.success) {
                        if (responseData.success) {
                            ShowResultDialog("success");
                        } else {
                            ShowResultDialog("error");
                        }
                    } else {
                        $(ChangePassword).attr("value", "修改密码");
                        $(ChangePassword).attr("disabled", "disabled");
                        switch (responseData.error) {
                        case "Captcha_Error":
                            $(Captcha_Error).text("请输入正确的验证码。");
                            break;
                        case "CurrentPassword_Error":
                            $(CurrentPassword_Error).text("该当前密码不正确。请确保使用与你的帐户对应的密码。");
                            $("#CaptchaImg").click();
                            break;
                        case "NewPassword_Error":
                            $(NewPassword_Error).text("该当前密码不正确。请确保使用与你的帐户对应的密码。");
                            $("#CaptchaImg").click();
                            break;
                        case "Cookie_Error":
                            window.location.replace("/Error/Cookie");
                            break;
                        }
                    }
                }
            });
        });
    });

    function ShowResultDialog(code) {
        $(function() {
            $("#dialog").dialog({
                title: "服务器消息",
                closeText: "关闭",
                modal: true,
                resizable: false,
                buttons: {
                    "确定": function() {
                        $(this).dialog("close");
                        if (code === "success") {
                            window.location.href = "/Account/Login";
                        }
                    }
                },
                beforeClose: function() {
                    if (code === "success") {
                        return false;
                    }
                    $(ChangePassword).removeAttr("disabled");
                    $(ChangePassword).attr("value", "修改密码");
                    return true;
                }
            });
            if (code === "success") {
                $("#success").text("密码修改成功!");
            } else {
                $("#error").text("密码修改失败!");
            }
        });
    };

    function HaveNoError() {
        var result = true;
        $(".form-horizontal :text").each(function() {
            if ($(this).val() === "") {
                result = false;
            }
        });
        $(".form-horizontal :password").each(function() {
            if ($(this).val() === "") {
                result = false;
            }
        });
        $("[id$='_Error']").each(function() {
            if ($(this).text() !== "") {
                result = false;
            }
        });
        return result;
    }
</script>
<div>
    <asp:PlaceHolder runat="server" ID="successMessage" Visible="false" ViewStateMode="Disabled">
        <p class="text-success"><%: SuccessMessage %></p>
    </asp:PlaceHolder>
</div>
<div class="row">
    <div class="col-md-12">
        <section id="passwordForm">
            <asp:PlaceHolder runat="server" ID="changePasswordHolder">
                <p>
                    你以 <strong><%: Context.User.Identity.Name %></strong> 身份登录。
                </p>
                <div class="form-horizontal">
                    <h4>更改密码</h4>
                    <hr/>
                    <asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="false">
                        <p class="text-danger">
                            <asp:Literal runat="server" ID="FailureText"/>
                        </p>
                    </asp:PlaceHolder>
                    <asp:ValidationSummary runat="server" ShowModelStateErrors="true" CssClass="text-danger"/>
                    <div class="form-group">
                        <asp:Label runat="server" ID="CurrentPasswordLabel" AssociatedControlID="CurrentPassword" CssClass="col-md-2 control-label">当前密码</asp:Label>
                        <div class="col-md-10">
                            <asp:TextBox runat="server" ID="CurrentPassword" TextMode="Password" CssClass="form-control"/>
                            <asp:Label ID="CurrentPassword_Error" runat="server" CssClass="text-danger"></asp:Label>
                        </div>
                    </div>
                    <div class="form-group">
                        <asp:Label runat="server" AssociatedControlID="NewPassword" CssClass="col-md-2 control-label">新密码</asp:Label>
                        <div class="col-md-10">
                            <asp:TextBox runat="server" ID="NewPassword" TextMode="Password" CssClass="form-control" MaxLength="16"/>
                            <asp:Label ID="NewPassword_Error" runat="server" CssClass="text-danger"></asp:Label>
                        </div>
                    </div>
                    <div class="form-group">
                        <asp:Label runat="server" AssociatedControlID="ConfirmNewPassword" CssClass="col-md-2 control-label">确认新密码</asp:Label>
                        <div class="col-md-10">
                            <asp:TextBox runat="server" ID="ConfirmNewPassword" TextMode="Password" CssClass="form-control" MaxLength="16"/>
                            <asp:Label ID="ConfirmNewPassword_Error" runat="server" CssClass="text-danger"></asp:Label>
                        </div>
                    </div>
                    <div class="form-group">
                        <asp:Label runat="server" AssociatedControlID="Captcha" CssClass="col-md-2 control-label">验证码</asp:Label>
                        <div class="col-md-10">
                            <img id="CaptchaImg" alt="点击更换图片" data-toggle="tooltip" data-placement="bottom" title="点击更换图片" src="/Handler/Captcha" class="form-control" style="border: none; cursor: pointer; height: 60px; width: 280px;"/>
                            <asp:TextBox data-toggle="tooltip" data-placement="bottom" title="验证码不区分大小写" runat="server" ID="Captcha" MaxLength="5" CssClass="form-control"/>
                            <asp:Label ID="Captcha_Error" runat="server" CssClass="text-danger"></asp:Label>
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input id="ChangePassword" type="button" value="更改密码" class="btn btn-primary btn-lg"/>
                        </div>
                    </div>
                </div>
            </asp:PlaceHolder>
        </section>
    </div>
</div>
<div id="dialog">
    <div id="message" style="font-size: medium"></div>
    <div id="success" class="text-success" style="font-size: medium"></div>
    <div id="error" class="text-danger" style="font-size: medium"></div>
</div>
</asp:Content>